package com.lanou3g.code0515.study;

import com.lanou3g.code0515.util.SmartUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

/**
 * If there are no bugs, it was created by Chen FengYao on 2018/5/15;
 * Otherwise, I don't know who created it either
 */

@WebServlet(name = "SmartUploadServlet",urlPatterns = "/smart")
public class SmartUploadServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");


        SmartUpload smartUpload = new SmartUpload(this);
        smartUpload.parse(request);//上传

        PrintWriter pw = response.getWriter();
        // 获取普通表单参数
        String code = smartUpload.getParameter("code");
        pw.println(code);
        pw.println("<br>");
        // 获取上传的文件信息
        Map<String,File> fileMap= smartUpload.getUploadedFiles();
        for (String fileName : fileMap.keySet()) {
            String filePath = fileMap.get(fileName).getAbsolutePath();
            pw.println(fileName + "::" + filePath);
            pw.println("<br>");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
